const app = getApp()
const $api = require('../../utils/api.js').API;
Page({
  data: {
      list:[],
      id:0,
      payOk:false,
      info:{},
  },
  onLoad: function(){
    this.getVip()
  },
  getVip:function(){
    var that = this
    var data ={}
    $api.get('vip/group',data).then(res => {
      //请求成功
      if(res.data.code == 1){
          that.setData({
            list:res.data.data,
            info:res.data.data[0],
            id:res.data.data[0].id
          })
      }
    })
  },
  onShareAppMessage: function(){
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  order:function(e){
    var index = e.currentTarget.dataset.index
    this.setData({
      id:this.data.list[index]['id'],
      info:this.data.list[index]
    })
  },
  payquery:function(){
    var that = this
    var data = {'uid':wx.getStorageSync('uid')}
    $api.get('member/info',data).then(res =>{
      //请求成功
      if(res.data.code == 1){
        that.getCount(res.data.data)
      }
    })
  },
  payclose:function(){
    this.setData({
      payOk:false
    })
  },
  getCount:function(info){
    if(parseFloat(this.data.info.score) > parseFloat(info.integral)){
      wx.showToast({
        title:'您的积分余额为'+info.integral+'积分不足,不能抵扣!!!',
        duration:2000,
        mask:true, 
        icon:'none',
      })
      return;
    }
    this.setData({
      payOk:true
    })
  },
  pay:function(){
    var data = {
      'company_uid':wx.getStorageSync('uid'),
      'payScore':this.data.info.score, //抵扣积分
      'vip_id':this.data.id,
      'num':this.data.info.num
    }
    var that = this
    $api.post('program/group/buy',data).then(res =>{
      if(res.data.code == 1){
        wx.showToast({
            title:'兑换成功',
            duration:3000,
            mask:true, 
            icon:'success',
            success:function(){
                setTimeout(function(){
                  wx.navigateBack()
                },3000) //延迟时间 这里是
            }
        })
      }else{
          wx.showToast({
              title:res.data.msg,
              duration:3000,
              mask:true, 
              icon:'none',
          })
          return;
      }
    })
  }
});